﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Collections;

namespace GeneralAdaptiveFramework
{
    public class Escape : ActiveComponent
    {
        private string collectPath = "path";
        private string analyzePath = "path";
        private string decidePath = "path";
        private string actPath = "path";
        public Escape(string _name)
            : base(_name)
        {
            initPort();
        }

        /**
         * Init two Provide ports and two Require ports
         */
        private void initPort()
        {
            interfaceList = new ArrayList();

            ProvideInterface provider01 = new ProvideInterface("PEscape01", this);

            interfaceList.Add(provider01);
        }

        /**
         * Receive data
         */
        public override void receiveData(Object data)
        {
            // add data to buffer
            buffer.Add(data);

            // call execute
            execute();
        }

        public override void execute()
        {
#if DEBUG
            Console.WriteLine("ESCAPE EXECUTE");
#endif
            addComponent(collectPath, "Collect");
            addComponent(analyzePath, "Escape Analyze");
            addComponent(decidePath, "Escape Decide");
            addComponent(actPath, "Act");

            addConnection(this.name, "Collect", "PEscape01", "RCollect01");
            addConnection("Collect", "Escape Analyze", "PCollect01", "RAnalyze01");
            addConnection("Escape Analyze", "Escape Decide", "PAnalyze01", "RDecide01");
            addConnection("Escape Decide", "Act", "PDecide01", "RAct01");

            // get data from buffer
            transferData("EXECUTE");
        }

        public override void stop()
        {
#if DEBUG
            Console.WriteLine("ESCAPE STOP");
#endif
            transferData("STOP");
            removeConnection(this.name, "Collect", "PEscape01", "RCollect01");
            removeConnection("Collect", "Escape Analyze", "PCollect01", "RAnalyze01");
            removeConnection("Escape Analyze", "Escape Decide", "PAnalyze01", "RDecide01");
            removeConnection("Escape Decide", "Act", "PDecide01", "RAct01");
        }
    }
}
